package com.bobi.tfca.ui.mainnavigation.message

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MessageAPI
import com.bobi.tfca.network.response.ChatRoomResponse
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class MessageFragmentVM : ViewModel() {

    val chatRooms by lazy {
        MutableLiveData(ArrayList<ChatRoomResponse>())
    }

    /**
     * 查询所有聊天室
     */
    fun getAllChatRooms(publicViewModel: PublicViewModel) {
        // 调用API查询所有聊天室
        publicViewModel.apply {
            request(MessageAPI::class.java).getAllChatRooms().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }
                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val chatRoomList = it.data?.data ?: arrayListOf()
                                chatRooms.value = chatRoomList
                            }
                        }
                    }
                }
            }
        }
    }

    fun getUnReadLetterNum(publicViewModel: PublicViewModel) {
        publicViewModel.apply {
            request(MessageAPI::class.java).getUnReadLetterNum().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }
                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val result = it.data?.data
                                if (result != null) {
                                    publicViewModel.newLetterNum.value = result
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    fun getUnReadNotificationNum(publicViewModel: PublicViewModel) {
        publicViewModel.apply {
            request(MessageAPI::class.java).getUnReadNotificationNum().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }
                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val result = it.data?.data
                                if (result != null) {
                                    publicViewModel.newNotificationNum.value = result
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}